package com.arch.dev.client.view;

import java.util.Date;
import java.util.Set;

import com.arch.dev.client.controller.BudgetController;
import com.arch.dev.client.vo.MonthVO;
import com.arch.dev.client.vo.YearVO;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.datepicker.client.DatePicker;
import com.tractionsoftware.gwt.user.client.ui.UTCDateBox;

public class DateSearchView extends Composite {
	
	public static enum Types{ALL, DATE, MONTH, YEAR};

	private static DateSearchViewUiBinder uiBinder = GWT
			.create(DateSearchViewUiBinder.class);

	interface DateSearchViewUiBinder extends UiBinder<Widget, DateSearchView> {
	}
	
	@UiField
	HorizontalPanel componentHP;
	
	UTCDateBox dateBox;
	
	Label dateLabel;

	ListBox monthlyListBox, yearlyListBox;
	
	/*@UiField(provided = true)*/
	Button searchButton;	
	
	private Set<YearVO> yearVOs;
	
	public Types filterType;
	

	public UTCDateBox getDateBox() {
		return dateBox;
	}

	public void setDateBox(UTCDateBox dateBox) {
		this.dateBox = dateBox;
	}

	Widget widget;
	public DateSearchView(Types type, Widget widget ) {
		this.widget = widget;
		searchButton = new Button("Search");
		initWidget(uiBinder.createAndBindUi(this));
		componentHP.setSpacing(5);

		dateLabel = new Label("Select date :");		

		DateTimeFormat dateFormat = DateTimeFormat
				.getFormat(PredefinedFormat.DATE_LONG);
		DatePicker datePicker = new DatePicker();
		dateBox = new UTCDateBox(datePicker, -1, new DateBox.DefaultFormat(dateFormat));
		
		Date currentDate = new Date();
		dateBox.setValue(UTCDateBox.date2utc(currentDate));		

		monthlyListBox = new ListBox();

		yearlyListBox = new ListBox();
		
		
		yearlyListBox.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {

				if (filterType == Types.MONTH) {

					for (YearVO yearVO : yearVOs) {

						if (yearVO.getId() == Integer
								.parseInt(getSelectedYear())) {

							populateMonthlyListBox(yearVO);

						}

					}

				} else {

					BudgetController.getInstance()
							.getYearlyData(Integer.parseInt(getSelectedYear()), false, 
									DateSearchView.this.widget);

				}

			}
		});

	

			monthlyListBox.addChangeHandler(new ChangeHandler() {

				public void onChange(ChangeEvent event) {

					BudgetController.getInstance().getMonthlyDataForYear(
							Integer.parseInt(getSelectedYear()),
							Integer.parseInt(getSelectedMonth()),
							false,
							DateSearchView.this.widget);

				}
			});

		
		
		
		processComponents( type);

	}

	public void processComponents(Types type){

		switch (type) {
		case ALL:
			componentHP.clear();
			componentHP.add(searchButton);
			BudgetController.getInstance().getAllValues(widget);
			filterType = Types.ALL;			
			break;
		case DATE:
			componentHP.clear();
			componentHP.add(dateLabel);
			componentHP.add(dateBox);
			componentHP.add(searchButton);
			filterType = Types.DATE;
			break;
		case MONTH:
			componentHP.clear();
			componentHP.add(yearlyListBox);
			componentHP.add(monthlyListBox);
			filterType = Types.MONTH;
			BudgetController.getInstance().getDateData(this);
			break;
		case YEAR:
			componentHP.clear();
			componentHP.add(yearlyListBox);
			filterType = Types.YEAR;
			BudgetController.getInstance().getDateData(this);
			break;
		}
	}
	
	HandlerRegistration handlerRegistration;
	
	public void addSearchButtonHandler(ClickHandler handler){
		if(handlerRegistration != null){
			handlerRegistration.removeHandler();
		}
		handlerRegistration = searchButton.addClickHandler(handler);
	}

	public void populateYearlyListBox(Set<YearVO> yearVOs) {

		this.yearVOs = yearVOs;

		yearlyListBox.clear();

		for (YearVO yearVO : yearVOs) {

			yearlyListBox.addItem(yearVO.getName(),
					Integer.toString(yearVO.getId()));

		}

		if (filterType == Types.YEAR) {

			BudgetController.getInstance().getYearlyData(
					Integer.parseInt(getSelectedYear()), false, widget);

		} else if (filterType == Types.MONTH)  {

			for (YearVO yearVO : yearVOs) {

				if (yearVO.getId() == Integer.parseInt(getSelectedYear())) {

					populateMonthlyListBox(yearVO);

				}

			}
		}

	}

	public void populateMonthlyListBox(YearVO yearVO) {

		monthlyListBox.clear();

		Set<MonthVO> monthVOs = yearVO.getMonths();

		for (MonthVO monthVO : monthVOs) {

			monthlyListBox.addItem(monthVO.getName(),
					Integer.toString(monthVO.getId()));

		}

		BudgetController.getInstance().getMonthlyDataForYear(
				Integer.parseInt(getSelectedYear()),
				Integer.parseInt(getSelectedMonth()), false, widget);

	}
	public String getSelectedYear() {

		return yearlyListBox.getValue(yearlyListBox.getSelectedIndex());

	}

	public String getSelectedMonth() {

		return monthlyListBox.getValue(monthlyListBox.getSelectedIndex());

	}
	
}
